package com.smart.wap.app;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.hutool.core.lang.Assert;
import com.smart.common.entities.ajax.AjaxResult;
import com.smart.common.entities.constant.SmartConstant;
import com.smart.common.entities.file.UploadResult;
import com.smart.service.FileService;
import com.smart.utils.FileUtils;
import com.smart.utils.MemberStp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;


/**
 * @author Administrator
 */

@Api(tags = "移动端-文件上传")
@RestController
@RequestMapping(SmartConstant.WAP_API_PREFIX + "upload")
@RequiredArgsConstructor
public class FileController {

    private final FileService fileService;

    @PostMapping(value = "avatar", headers = SmartConstant.FILE_CONTENT_TYPE)
    @ApiOperation(value = "上传头像")
    @SaCheckLogin(type = MemberStp.TYPE)
    public AjaxResult<UploadResult> uploadAvatar(@RequestParam("file") MultipartFile file) {
        Assert.isTrue(FileUtils.isImage(file), "上传的文件不是图片格式");
        boolean limitCheck = FileUtils.checkFileSize(file.getSize(), 1024, "K");
        Assert.isTrue(limitCheck, "上传的图片大小不能超过1MB");
        UploadResult uploadResult = fileService.uploadImg(file, "member/avatar", "");
        return AjaxResult.data(uploadResult);
    }



    @PostMapping(value = "image", headers = SmartConstant.FILE_CONTENT_TYPE)
    @ApiOperation(value = "上传图片")
    @SaCheckLogin(type = MemberStp.TYPE)
    public AjaxResult<UploadResult> uploadImage(@RequestParam("file") MultipartFile file) {
        Assert.isTrue(FileUtils.isImage(file), "上传的文件不是图片格式");
        boolean limitCheck = FileUtils.checkFileSize(file.getSize(), 1024, "K");
        Assert.isTrue(limitCheck, "上传的图片大小不能超过1MB");
        UploadResult uploadResult = fileService.uploadImg(file, "image", "");
        return AjaxResult.data(uploadResult);
    }


    @PostMapping(value = "audio", headers = SmartConstant.FILE_CONTENT_TYPE)
    @ApiOperation(value = "上传音频")
    @SaCheckLogin(type = MemberStp.TYPE)
    public AjaxResult<UploadResult> uploadAudio(@RequestParam("file") MultipartFile file) {
        UploadResult uploadResult = fileService.uploadFile(file, "member/audio", "");
        return AjaxResult.data(uploadResult);
    }


}
